package reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {
	public String name;
	protected List<String> stringList = new ArrayList<String>();
	public List<String> getStringList(){
	    return this.stringList;
	  }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Demo1(String name) {
		this.name = name;
	}

	public Demo1() {

	}

	public void t1() throws Exception, IllegalAccessException {
		Demo1 demo1 = new Demo1();
		Class<Demo1> dclazz = Demo1.class;

		for (Method mt : dclazz.getMethods()) {
			System.out.println(mt.toString() + "======method");
		}
		for (Constructor<?> string : dclazz.getConstructors()) {
			System.out.println(string.toString() + "构造函数");
		}
		System.out.println(Modifier.isAbstract(dclazz.getModifiers())
				+ "====修饰符是否isAbstract modifiers");
		System.out.println(Modifier.isPublic(dclazz.getModifiers())
				+ "====修饰符是否isPublic modifiers");
		// 获取特定的构造函数
		Constructor<Demo1> cons = dclazz.getConstructor();
		demo1 = cons.newInstance();
		System.out.println(demo1.t2());

		// 获取包
		Package pkg = dclazz.getPackage();
		System.out.println(pkg.getName());

		// 获取superclass
		Class<? super Demo1> c = dclazz.getSuperclass();

		// 获取接口
		Class<?>[] inter = dclazz.getInterfaces();

		// 获取属性
		Field[] fields = dclazz.getFields();

		Annotation[] annotations = dclazz.getAnnotations();
		for (Annotation annotation : annotations) {
			System.out.println(annotation.getClass().toString());
		}
		// Class对象实例化
		demo1 = dclazz.newInstance();
		System.out.println(demo1.t2());
	}

	public int t2() {
		return 1;
	}

	public static void main(String[] args) throws Exception {
		t5();

	}

	public static void t3() throws Exception, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		// 构造函数
		Class<Demo1> clazz = Demo1.class;
		@SuppressWarnings("unchecked")
		Constructor<Demo1>[] constructors = (Constructor<Demo1>[]) clazz
				.getConstructors();
		for (Constructor<?> constructor : constructors) {
			System.out.println(constructor.getModifiers());
			System.out.println(constructor.getName());
			if (constructor.getParameters() != null
					&& constructor.getParameters().length != 0) {
				Demo1 demo1 = (Demo1) constructor.newInstance("goood");
				System.out.println(demo1.getName());
			}
		}
	}

	public static void t4() throws Exception {
		Class<Demo1> clazz = Demo1.class;
		Demo1 demo = new Demo1();
		for (Field field : clazz.getFields()) {
			System.out.println(field.getName());
			field.set(demo, "aaaa");
			System.out.println(field.get(demo));
		}
	}

	public static void t5() throws Exception {
		Class<Demo1> clazz = Demo1.class;
		Method method = clazz.getMethod("getStringList");
		Field field=clazz.getField("name");
		//获取返回值的类型
		Type returnType = method.getGenericReturnType();
		//获取类型的类型
		Type genericFieldType = field.getGenericType();
		//获取方法参数的类型
		Type[] genericParameterTypes = method.getGenericParameterTypes();
		
		System.out.println(returnType.getTypeName()+"typeName");

		if (returnType instanceof ParameterizedType) {
			ParameterizedType type = (ParameterizedType) returnType;
			Type[] typeArguments = type.getActualTypeArguments();
			for (Type typeArgument : typeArguments) {
				Class<?> typeArgClass = (Class<?>) typeArgument;
				System.out.println("typeArgClass = " + typeArgClass);
			}
		}else{
			System.out.println("gooo");
		}

	}
}
/**
 * Modifier.isAbstract(int modifiers) Modifier.isFinal(int modifiers)
 * Modifier.isInterface(int modifiers) Modifier.isNative(int modifiers)
 * Modifier.isPrivate(int modifiers) Modifier.isProtected(int modifiers)
 * Modifier.isPublic(int modifiers) Modifier.isStatic(int modifiers)
 * Modifier.isStrict(int modifiers) Modifier.isSynchronized(int modifiers)
 * Modifier.isTransient(int modifiers) Modifier.isVolatile(int modifiers)
 */
